import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleXmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.htmlcleaner.XmlSerializer;


public class ReadLanacionAndSerialize {

	/**
	 * @param args
	 */
	private static final String ITEMAUTO_XPATH = "//div[@class='itemAutos']";
	public static void main(String[] args) throws XPatherException, MalformedURLException, IOException, ValidityException, ParsingException {
		// TODO Auto-generated method stub
		if (args.length == 0) {
			System.err.println("Usage: java ReadLanacionAndTranslate url");
		    return;
		}
		
		// this is where the HtmlCleaner comes in, I initialize it here
        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);
        props.setOmitXmlDeclaration(true);
        
        TagNode node = cleaner.clean(new URL(args[0]));
        //TagNode node = cleaner.clean(args[0]);
		
		Object[] div_nodes = node.evaluateXPath(ITEMAUTO_XPATH);
			
		String result_xml = "<?xml version='1.0'?><root>";
		XmlSerializer serializer = new SimpleXmlSerializer(props);
		for (int i = 0; i < div_nodes.length; i++) {
			result_xml+=serializer.getAsString((TagNode) div_nodes[i]);									
		}
		result_xml+="</root>";
		InputStream is_xml = new ByteArrayInputStream(result_xml.getBytes("UTF-8"));
		Document doc = (new Builder()).build(is_xml);		
		try {
            OutputStream out = new FileOutputStream("transito.xml");
            out = new BufferedOutputStream(out);
            Serializer xom_serializer = new Serializer(out, "ISO-8859-1");
            xom_serializer.write(doc);
        }  
        catch (IOException ex) {
            System.err.println("This shouldn't happen for Latin-1!");
        }


		

	}

}
